package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IColorDAO;
import com.twentyOne.ClothingLand.vo.Color;

public class ColorDAO extends BaseDAO implements IColorDAO {

	@Override
	public void addColor(Color color) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(color);
		tx.commit();
		session.close();
		
	}

	@Override
	public void deleteColor(Color color) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete Color as c where c.colorId = "+color.getColorId()+"";
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public void modifyColor(Color color) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Color set name=:name, description=:description where colorId=:colorId";
		Query query = session.createQuery(hql);
		query.setString("name", color.getName());
		query.setString("description", color.getDescription());
		query.setInteger("colorId", color.getColorId());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public List<Color> searchAllColor() {
		
		Session session = getSession();
		String hql = "from Color";
		Query query = session.createQuery(hql);
		List<Color> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public Color searchByID(int colorId) {

		Session session = getSession();
		String hql = "from Color as c where c.colorId=" + colorId;
		Query query = session.createQuery(hql);
		List<Color> list = query.list();
		session.close();
		if (list.size() != 0) {
			Color color = list.get(0);
			return color;
		}else {
			return null;
		}
		
	}

}
